package com.sc.controller.csm;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.domain.Character;
import com.sc.dto.CharacterDTO;
import com.sc.result.Result;
import com.sc.service.csm.ICharacterService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "设置-角色管理")
@CrossOrigin
@RestController
@RequestMapping("/character")
@RequiredArgsConstructor
public class CharacterController {

    private final ICharacterService characterService;

    @PostMapping("/add")
    public Result add(@RequestBody Character character) {
        character.setAddTime(LocalDateTime.now());
        character.setCharacterNum(0);
        try {
            return Result.success("添加成功", characterService.save(character));
        } catch (DuplicateKeyException e) {
            return Result.error("角色名称重复，换一个试试");
        }
    }

    @DeleteMapping("/deleteById")
    public Result deleteById(Integer id) {
        return Result.success("删除成功", characterService.removeById(id));
    }

    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        return Result.success("删除成功", characterService.removeByIds(ids));
    }

    @PutMapping("/update")
    public Result update(@RequestBody Character character) {
        try {
            return Result.success("修改成功", characterService.updateById(character));
        } catch (DuplicateKeyException e) {
            return Result.error("角色名称重复，换一个试试");
        }
    }

    @GetMapping("/selectById")
    public Result<Character> selectById(Integer id) {
        return Result.success(characterService.getById(id));
    }

    @PostMapping("/pageQuery")
    public Result<Page<Character>> pageQuery(@RequestBody CharacterDTO characterDTO) {
        return Result.success(characterService.pageQuery(characterDTO));
    }

    @GetMapping("/getAll")
    public Result<List<Character>> getAll() {
        return Result.success(characterService.list());
    }
}
